package com.six.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.six.bean.Product;
import com.six.bean.Supply;
import com.six.service.ProductService;
import com.six.util.Result;
import com.six.service.SupplyService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("supply")
//跨域问题
@CrossOrigin
public class SupplyController {
    //依赖注入
    @Autowired
    SupplyService supplyService;
    ProductService productService;

//    @RequestMapping("list")
//    public List<Admin> getlist1(){
//        List<Admin> list=adminService.list();
//        return list;
//    }
    @GetMapping("list")
    public Result getList(){
        List<Supply> list = supplyService.list();
        for (Supply item : list) {
            Product product=productService.getById(item.getProduct().getPid());
            item.setProduct(product);
        }
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page,@RequestParam("searchText") String searchText){
        System.out.println(searchText);
        Page result= null;
        if(searchText==null||searchText.trim().isEmpty()){
            result=supplyService.page(page);
        }else {
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.like("supplyname",searchText);
            wrapper.or();
            wrapper.like("pid",searchText);
            wrapper.or();
            wrapper.like("supplyid",searchText);
            wrapper.or();
            wrapper.like("indate",searchText);

            result=supplyService.page(page,wrapper);
        }
        return Result.success(result);
    }
    @PostMapping
    public Result add(@RequestBody Supply supply){
        supplyService.save(supply);
        Supply supply1=supplyService.getById(supply.getSupplyid());
        return Result.success("保存成功",supply1);
    }
    @DeleteMapping("/{supplyid}")
    public Result delete(@PathVariable("supplyid") Integer supplyid){
        boolean b = supplyService.removeById(supplyid);
        if(b){
            return Result.success("删除成功",null);
        }else {
            return Result.fail(201,"删除失败");
        }
    }
    @PutMapping
    public Result edit(@RequestBody Supply supply){
        boolean b = supplyService.saveOrUpdate(supply);
        Supply supply1=supplyService.getById(supply.getSupplyid());
        if(b){
            return Result.success("保存成功",supply1);
        }else {
            return Result.fail(201,"修改失败");
        }
    }





}
